<?php
/**
 * the api wrapper
 * 
 * @author tom.wang<tom.wang.sz@qq.com>
 */
class RenrenApi {
	private $access_token;
	private $call_id;
	private $supportFormats = array('xml', 'json');
	
	/**
	 * set session key
	 * if you get session key through other framework, you can use only api section in this framework
	 */
	public function setAccessToken($access_token) {
		$this->access_token = $access_token;
	}
	
	/**
	 * set call id
	 * no need to call commonly
	 */
	public function setCallId($call_id) {
		$this->call_id = $call_id;
	}
	
	/**
	 * get api version
	 */
	public function getVersion() {
		return '1.0';
	}
	
	/**
	 * the entry to call api
	 */
	public function api($params = array(), $gzip = false) {
		$params['call_id'] = $this->getCallId();
		$params['v'] = $this->getVersion();
		$params['access_token'] = $this->getAccessToken();
		if(!isset($params['format']) || !in_array($params['format'], $this->supportFormats)) {
			$params['format'] = RenrenClient::getCfg('format');
		}
		
		$params['sig'] = RenrenUtil::getSig($params);
		$url = RenrenClient::getCfg('api_url');
		$response = RenrenUtil::http($url, 'POST', $params, $gzip);
		
		return $response;
	}
	
	private function getAccessToken() {
		return $this->access_token;
	}
	
	private function getCallId() {
		if(null !== $this->call_id) $call_id = $this->call_id;
		else $call_id = array_pop(explode(' ', microtime()));
		
		return $call_id;
	}
}
?>